from django.shortcuts import render
from drf_haystack.viewsets import HaystackViewSet
from rest_framework.generics import ListAPIView
from rest_framework.filters import OrderingFilter

from goods.models import SKU
from .serializers import SKUListSerializer, SKUIndexSerializer


class SKUListAPIView(ListAPIView):
    """
    SKU列表数据
    """
    # 指定排序字段,注意下面的元组，由逗号
    filter_backends = (OrderingFilter,)
    ordering_fields = ['create_time', 'price', 'sales']

    serializer_class = SKUListSerializer

    def get_queryset(self):
        category_id = self.kwargs['category_id']

        return SKU.objects.filter(category_id=category_id, is_launched=True)

class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]

    serializer_class = SKUIndexSerializer